package com.itheima.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dubbo.api.UserApi;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.dubbo.api.mongo.FriendApi;
import com.itheima.server.interceptor.UserHolder;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;


    public UserInfoVo findUserInfoByHuanxin(String huanxinId) { //hx用户的id
        //1. 根据环信的id切割出user的Id
        Long userId = Long.valueOf(huanxinId.substring(2));

        //2. 根据UserId查找UserInfo
        UserInfo userInfo = userInfoApi.findById(userId);

        //3. 把UserINfo转换为UserInfo的Vo
        UserInfoVo vo = new UserInfoVo();
        if(userInfo!=null) {
            BeanUtils.copyProperties(userInfo, vo);
            vo.setAge(userInfo.getAge() + "");
        }
        return vo;

    }

    /**
     * 添加好友
     * @param friendId
     */
    public void contacts(Long friendId) {
        //1. 获取当前登陆者
        Long userId = UserHolder.getId();
        //2. 数据库中添加为好友
        friendApi.save(userId,friendId);
        //3. 环信添加为好友
        huanXinTemplate.contactUsers(Constants.HX_USER_PREFIX+userId,Constants.HX_USER_PREFIX+friendId);

    }

    /**
     * 查看好友列表
     * @param page  当前页
     * @param pagesize  页面大小
     * @param keyword  搜索昵称
     * @return
     */
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1. 根据用户id查询好友的分页对象  PageResult  pageReuslt里面存储的Friend
        Long userId = UserHolder.getId();
        List<Friend> friendList = friendApi.findByUserId(userId);

        //2. 提取FriendId  2,3,4,5...
        List<Long> friendIds = CollUtil.getFieldValues(friendList, "friendId", Long.class);

        //3. 根据好友的id查询分页的对象 Page
        Page<UserInfo> pageInfo = new Page<>(page,pagesize);
        //调用mybatis-plus分页功能
        pageInfo=  userInfoApi.findFriendByPage(pageInfo,friendIds,keyword);

        //4. 得到page里面的UserInfo，然后转换COntactVo
        List<UserInfo> userInfoList = pageInfo.getRecords();
        List<ContactVo> voList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            ContactVo vo = ContactVo.init(userInfo);
            voList.add(vo);
        }
        return new PageResult(page.longValue(),pagesize.longValue(), pageInfo.getTotal(),voList);
    }
}
